﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XBoshiGFS.forms
{
    /// <summary>
    /// 斗渠信息网维护界面
    /// </summary>
    public partial class WaterCourseForm : Form
    {

        public WaterCourseSet waterCourse { get; set; }
        public XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();  


        public WaterCourseForm()
        {
            InitializeComponent();

            waterCourse = new WaterCourseSet();
            waterCourse.strName = "新建斗渠";  
            waterCourse.Id = Guid.NewGuid().ToString();
            context.AddToWaterCourseSet(waterCourse);
            waterCourseSetBindingSource.DataSource = waterCourse;
            cmbStation.DataSource = (from p in context.StationSet select p);

        }

       public WaterCourseForm(WaterCourseSet  p_waterCursor)
        {
            InitializeComponent();


            waterCourse = (from p in context.WaterCourseSet where p.Id == p_waterCursor.Id select p).FirstOrDefault();
            waterCourseSetBindingSource.DataSource = waterCourse;
            context.Attach(waterCourse);


            var segment = waterCourse.SegmentCourseSet;
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;

            //  var station = segment.StationSet; 
            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }


       public WaterCourseForm(SegmentCourseSet  p_segment)
        {
            InitializeComponent();

            waterCourse = new WaterCourseSet();
            waterCourse.strName = "新建斗渠";
            waterCourse.Id = Guid.NewGuid().ToString();
            context.AddToWaterCourseSet(waterCourse);
            waterCourseSetBindingSource.DataSource = waterCourse;


            var segment = (from p in context.SegmentCourseSet where p.Id == p_segment.Id select p).FirstOrDefault();
            waterCourse.SegmentCourseSet = segment;
           
           
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;


            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }





        private void btnOK_Click(object sender, EventArgs e)
        {
            if (TBName.Text == "")
            {
                MessageBox.Show("请填写名称！", "提示");
                TBName.Focus();
                return;
            }
            if (TBCode.Text == "")
            {
                MessageBox.Show("请填写代码！", "提示");
                TBCode.Focus();
                return;
            }
            if (String.IsNullOrWhiteSpace(waterCourse.SegmentCourseId ))
            {
                MessageBox.Show("请选择上层单位！", "提示");
                return;
            }

            var code = (from p in context.WaterCourseSet  where p.strCode == TBCode.Text select p);
            var maxCode = "";
            try
            {
                var sugesstNo = (from p in context.WaterCourseSet select p.strCode).Cast<Int32>().Max();
                maxCode = String.Format("{0}", (sugesstNo + 1)); //(int.Parse(sugest.FirstOrDefault().ToString()) + 1).ToString();
            }
            catch
            {
            } 
            if (code.Count() == 0)
            {
            }
            else
            {
                if (code.Count() == 1 && code.First().Id == waterCourse.Id)
                {
                }
                else
                {
                    MessageBox.Show("代码重复！建议编码为：" + maxCode, "提示");
                    TBCode.Focus();
                    return;
                }
            }
               
                //设置sc的相关信息
            if (waterCourse.EntityKey == null)
            {

             //   context.SegmentCourseSet.AddObject(segment);
            }
            else
            {
             //   segment.Id = Guid.NewGuid().ToString(); 
             //    context.SegmentCourseSet.Attach(segment);
            }
            context.SaveChanges();
            this.DialogResult = DialogResult.Yes; 
 
        }

        private void SegmentForm_Load(object sender, EventArgs e)
        {

        }
        private void cmbStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbSegment.DataSource = (from p in context.SegmentCourseSet where p.StationId == strID orderby p.strName select p);
        }

        private void cmbSegment_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            waterCourse.SegmentCourseId = strID.ToString(); 
        }

    }
}
